//
//  WeatherViewController.m
//  Weather
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "WeatherViewController.h"
#import "Weather.h"
#import "CityTableViewController.h"


@interface WeatherViewController ()



@end

@implementation WeatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.weatherid = @"CHBJ000000";
    self.city = @"%E5%8C%97%E4%BA%AC";

    
    self.title = @"出行天气";
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.cmstop.net/mobile/index.php?app=mobile&controller=weather&action=weather&weatherid=%@&cityname=%@",self.weatherid,self.city]]];
    
      [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
          // 解析
          NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
             NSDictionary *dic1 = dic[@"data"];
              Weather *weather = [[Weather alloc] init];
              weather.city = [dic1 objectForKey:@"city"];
              weather.week =[dic1 objectForKey:@"week"];
              weather.date_1 = [dic1 objectForKey:@"date_l"];
              weather.weather1 = [dic1 objectForKey:@"weather1"];
              weather.wind1 = [dic1 objectForKey:@"wind1"];
              weather.temp1 = [dic1 objectForKey:@"temp1"];
              
              weather.weather2 = [dic1 objectForKey:@"weather2"];
              weather.wind2 = [dic1 objectForKey:@"wind2"];
              weather.temp2 = [dic1 objectForKey:@"temp2"];
              
              weather.weather3 = [dic1 objectForKey:@"weather3"];
              weather.wind3 = [dic1 objectForKey:@"wind3"];
              weather.temp3 = [dic1 objectForKey:@"temp3"];
              
              
              self.cityLabel.text = weather.city;
              self.weekLabel.text = weather.week;
              self.date_1Label.text = weather.date_1;
              self.weather1Label.text = weather.weather1;
              self.wind1Label.text = weather.wind1;
              self.temp1Label.text = weather.temp1;
              self.weather2Label.text = weather.weather2;
              self.wind2Label.text = weather.wind2;
              self.temp2Label.text = weather.temp2;
              self.weather3Label.text = weather.weather3;
              self.wind3Label.text = weather.wind3;
              self.temp3Label.text = weather.temp3;
              
              self.temp1_1Label.text = weather.temp1;
              self.weather1_1Label.text = weather.weather1;
          
          
      }];
    
    
    NSDate *date = [NSDate date];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy年MM月dd日 HH点mm分ss秒"];
    
    NSString *dateStr = [formatter stringFromDate:date];
    NSLog(@"dateStr = %@",dateStr);
    
    NSString *str = [dateStr substringWithRange:NSMakeRange(5, 6)];
    NSString *str1 = [dateStr substringWithRange:NSMakeRange(12, 6)];
    
    self.dateLabel.text = str;
    self.timeLabel.text = str1;
    self.date1Label.text = str;
    
     UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"城市" style:UIBarButtonItemStylePlain target:self action:@selector(findCityButton:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
    
}










- (IBAction)findCityButton:(id)sender {
    
    CityTableViewController *cityTVC = [[CityTableViewController alloc] init];
//    [self.navigationController pushViewController:cityTVC animated:YES];
    cityTVC.myBlock = ^(NSString *s1,NSString *s2) {
        
        self.weatherid = s1;
        self.city = s2;
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.cmstop.net/mobile/index.php?app=mobile&controller=weather&action=weather&weatherid=%@&cityname=%@",self.weatherid,self.city]]];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            // 解析
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dic1 = dic[@"data"];
            Weather *weather = [[Weather alloc] init];
            weather.city = [dic1 objectForKey:@"city"];
            weather.week =[dic1 objectForKey:@"week"];
            weather.date_1 = [dic1 objectForKey:@"date_l"];
            weather.weather1 = [dic1 objectForKey:@"weather1"];
            weather.wind1 = [dic1 objectForKey:@"wind1"];
            weather.temp1 = [dic1 objectForKey:@"temp1"];
            
            weather.weather2 = [dic1 objectForKey:@"weather2"];
            weather.wind2 = [dic1 objectForKey:@"wind2"];
            weather.temp2 = [dic1 objectForKey:@"temp2"];
            
            weather.weather3 = [dic1 objectForKey:@"weather3"];
            weather.wind3 = [dic1 objectForKey:@"wind3"];
            weather.temp3 = [dic1 objectForKey:@"temp3"];
            
            
            self.cityLabel.text = weather.city;
            self.weekLabel.text = weather.week;
            self.date_1Label.text = weather.date_1;
            self.weather1Label.text = weather.weather1;
            self.wind1Label.text = weather.wind1;
            self.temp1Label.text = weather.temp1;
            self.weather2Label.text = weather.weather2;
            self.wind2Label.text = weather.wind2;
            self.temp2Label.text = weather.temp2;
            self.weather3Label.text = weather.weather3;
            self.wind3Label.text = weather.wind3;
            self.temp3Label.text = weather.temp3;
            
            self.temp1_1Label.text = weather.temp1;
            self.weather1_1Label.text = weather.weather1;
            
            
        }];
        
        
        NSDate *date = [NSDate date];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        
        [formatter setDateFormat:@"yyyy年MM月dd日 HH点mm分ss秒"];
        
        NSString *dateStr = [formatter stringFromDate:date];
        NSLog(@"dateStr = %@",dateStr);
        
        NSString *str = [dateStr substringWithRange:NSMakeRange(5, 6)];
        NSString *str1 = [dateStr substringWithRange:NSMakeRange(12, 6)];
        
        self.dateLabel.text = str;
        self.timeLabel.text = str1;
        self.date1Label.text = str;
        
        
        
    };
    
    
    
    [self presentViewController:cityTVC animated:YES completion:nil];

    
}


- (IBAction)refrashButton:(id)sender {
    

    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
